package com.topcent.tioxyc.xxljob;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.topcent.tioxyc.mapper.CustomerInfoMapper;
import com.topcent.tioxyc.pojo.CustomerInfo;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author: weidian
 * @date: 2024/10/31 11:40
 * @Description: 客户等级划分
 */
@Slf4j
@Component
public class XiaoShouE {


    @Autowired
    private CustomerInfoMapper customerInfoMapper;

    @XxlJob("XiaoShouE")
    public void xiao(){
        customerInfoMapper.truncateTable();
        String startDate = "2023-07-01";
        String endDate = "2024-07-01";
        String end = "2025-07-01";
        String end26 = "2026-07-01";
        // 计算出23年七月到24年七月月期间，客户的等级
        List<CustomerInfo> customerInfos = customerInfoMapper.selectCustomerInfo(startDate, endDate);
        List<CustomerInfo> selected = customerInfoMapper.selectCustomerInfos(startDate, endDate);
        // 将 customerInfoList 的数据追加到 customerInfos 的末尾
        if (selected != null && !selected.isEmpty()) {
            customerInfos.addAll(selected);
        }
        for (CustomerInfo customerInfo : customerInfos) {
            BigDecimal salesAmount = customerInfo.getFY23_24SalesAmount();
            // 设置 FY23-24 客户等级
            if (salesAmount.compareTo(new BigDecimal("10000000")) >= 0) {
                customerInfo.setCustomerLevelFY23_24("GSKA");
            } else if (salesAmount.compareTo(new BigDecimal("3000000")) >= 0) {
                customerInfo.setCustomerLevelFY23_24("SKA");
            } else if (salesAmount.compareTo(new BigDecimal("1000000")) >= 0) {
                customerInfo.setCustomerLevelFY23_24("KA");
            } else if (salesAmount.compareTo(new BigDecimal("300000")) >= 0) {
                customerInfo.setCustomerLevelFY23_24("PA");
            } else {
                customerInfo.setCustomerLevelFY23_24("AA");
            }
        }

        //计算出24年七月到25年七月月期间，客户的等级
        List<CustomerInfo> customerInfos1 = customerInfoMapper.selectCustomerInfo(endDate, end);
        for (CustomerInfo customerInfo1 : customerInfos1) {
            boolean found = false;
            for (CustomerInfo customerInfo2 : customerInfos) {
                if (customerInfo1.getCustomerCode().equals(customerInfo2.getCustomerCode())) {
                    found = true;
                    break; // 提前结束内层循环
                }
            }
            if (!found) {
                customerInfos.add(customerInfo1); // 添加到 customerInfos
            }
        }


        for (CustomerInfo customerInfo : customerInfos1) {
            for (CustomerInfo info : customerInfos) {
                if (customerInfo.getCustomerCode().equals(info.getCustomerCode())){
                    info.setFY24_25SalesAmount(customerInfo.getFY23_24SalesAmount());
                    BigDecimal salesAmount25 = info.getFY24_25SalesAmount();
                    // 设置 FY24-25 客户等级
                    if (salesAmount25.compareTo(new BigDecimal("10000000")) >= 0) {
                        info.setCustomerLevelFY24_25("GSKA");
                    } else if (salesAmount25.compareTo(new BigDecimal("3000000")) >= 0) {
                        info.setCustomerLevelFY24_25("SKA");
                    } else if (salesAmount25.compareTo(new BigDecimal("1000000")) >= 0) {
                        info.setCustomerLevelFY24_25("KA");
                    } else if (salesAmount25.compareTo(new BigDecimal("300000")) >= 0) {
                        info.setCustomerLevelFY24_25("PA");
                    } else {
                        info.setCustomerLevelFY24_25("AA");
                    }

                    break;
                }
            }
        }

        //计算出25年七月到26年七月月期间，客户的等级
        List<CustomerInfo> customerInfos2 = customerInfoMapper.selectCustomerInfo(end, end26);
        for (CustomerInfo customerInfo2 : customerInfos2) {
            boolean found = false;
            for (CustomerInfo customerInfo3 : customerInfos) {
                if (customerInfo2.getCustomerCode().equals(customerInfo3.getCustomerCode())) {
                    found = true;
                    break; // 提前结束内层循环
                }
            }
            if (!found) {
                customerInfos.add(customerInfo2); // 添加到 customerInfos
            }
        }


        for (CustomerInfo customerInfo : customerInfos2) {
            for (CustomerInfo info : customerInfos) {
                if (customerInfo.getCustomerCode().equals(info.getCustomerCode())){
                    info.setFY25_26SalesAmount(customerInfo.getFY23_24SalesAmount());
                    BigDecimal salesAmount26 = info.getFY25_26SalesAmount();
                    // 设置 FY25-26 客户等级
                    if (salesAmount26.compareTo(new BigDecimal("10000000")) >= 0) {
                        info.setCustomerLevelFY25_26("GSKA");
                    } else if (salesAmount26.compareTo(new BigDecimal("3000000")) >= 0) {
                        info.setCustomerLevelFY25_26("SKA");
                    } else if (salesAmount26.compareTo(new BigDecimal("1000000")) >= 0) {
                        info.setCustomerLevelFY25_26("KA");
                    } else if (salesAmount26.compareTo(new BigDecimal("300000")) >= 0) {
                        info.setCustomerLevelFY25_26("PA");
                    } else {
                        info.setCustomerLevelFY25_26("AA");
                    }

                    break;
                }
            }
        }
        //计算出23年7月到26年六月每个月的销售额36个月
        String meiDate="2023/07/01";
        String meiend="2023/07/31";
        for (int i = 0; i < 36; i++) {
            Date date1 = DateUtil.parse(meiDate);
            Date date2 = DateUtil.parse(meiend);
            DateTime newDate1 = DateUtil.offsetMonth(date1, i);
            DateTime newDate2 = DateUtil.offsetMonth(date2, i);
            String dateStr1 = newDate1.toString("yyyy/MM/dd");
            String dateStr2 = newDate2.toString("yyyy/MM/dd");
            List<CustomerInfo> customerInfoList = customerInfoMapper.selectCustomerInfo(dateStr1, dateStr2);

            switch (i) {
                case 0:
                    for (CustomerInfo customerInfo : customerInfoList) {
                        for (CustomerInfo info : customerInfos) {
                            if (customerInfo.getCustomerCode().equals(info.getCustomerCode())) {
                                info.setSalesAmountJul23(customerInfo.getFY23_24SalesAmount());
                                break;
                            }
                        }
                    }
                    break;
                case 1:
                    for (CustomerInfo customerInfo : customerInfoList) {
                        for (CustomerInfo info : customerInfos) {
                            if (customerInfo.getCustomerCode().equals(info.getCustomerCode())) {
                                info.setSalesAmountAug23(customerInfo.getFY23_24SalesAmount());
                                break;
                            }
                        }
                    }
                    break;
                case 2:
                    for (CustomerInfo customerInfo : customerInfoList) {
                        for (CustomerInfo info : customerInfos) {
                            if (customerInfo.getCustomerCode().equals(info.getCustomerCode())) {
                                info.setSalesAmountSep23(customerInfo.getFY23_24SalesAmount());
                                break;
                            }
                        }
                    }
                    break;
                case 3:
                    for (CustomerInfo customerInfo : customerInfoList) {
                        for (CustomerInfo info : customerInfos) {
                            if (customerInfo.getCustomerCode().equals(info.getCustomerCode())) {
                                info.setSalesAmountOct23(customerInfo.getFY23_24SalesAmount());
                                break;
                            }
                        }
                    }
                    break;
                case 4:
                    for (CustomerInfo customerInfo : customerInfoList) {
                        for (CustomerInfo info : customerInfos) {
                            if (customerInfo.getCustomerCode().equals(info.getCustomerCode())) {
                                info.setSalesAmountNov23(customerInfo.getFY23_24SalesAmount());
                                break;
                            }
                        }
                    }
                    break;
                case 5:
                    for (CustomerInfo customerInfo : customerInfoList) {
                        for (CustomerInfo info : customerInfos) {
                            if (customerInfo.getCustomerCode().equals(info.getCustomerCode())) {
                                info.setSalesAmountDec23(customerInfo.getFY23_24SalesAmount());
                                break;
                            }
                        }
                    }
                    break;
                case 6:
                    for (CustomerInfo customerInfo : customerInfoList) {
                        for (CustomerInfo info : customerInfos) {
                            if (customerInfo.getCustomerCode().equals(info.getCustomerCode())) {
                                info.setSalesAmountJan24(customerInfo.getFY23_24SalesAmount());
                                break;
                            }
                        }
                    }
                    break;
                case 7:
                    for (CustomerInfo customerInfo : customerInfoList) {
                        for (CustomerInfo info : customerInfos) {
                            if (customerInfo.getCustomerCode().equals(info.getCustomerCode())) {
                                info.setSalesAmountFeb24(customerInfo.getFY23_24SalesAmount());
                                break;
                            }
                        }
                    }
                    break;
                case 8:
                    for (CustomerInfo customerInfo : customerInfoList) {
                        for (CustomerInfo info : customerInfos) {
                            if (customerInfo.getCustomerCode().equals(info.getCustomerCode())) {
                                info.setSalesAmountMar24(customerInfo.getFY23_24SalesAmount());
                                break;
                            }
                        }
                    }
                    break;
                case 9:
                    for (CustomerInfo customerInfo : customerInfoList) {
                        for (CustomerInfo info : customerInfos) {
                            if (customerInfo.getCustomerCode().equals(info.getCustomerCode())) {
                                info.setSalesAmountApr24(customerInfo.getFY23_24SalesAmount());
                                break;
                            }
                        }
                    }
                    break;
                case 10:
                    for (CustomerInfo customerInfo : customerInfoList) {
                        for (CustomerInfo info : customerInfos) {
                            if (customerInfo.getCustomerCode().equals(info.getCustomerCode())) {
                                info.setSalesAmountMay24(customerInfo.getFY23_24SalesAmount());
                                break;
                            }
                        }
                    }
                    break;
                case 11:
                    for (CustomerInfo customerInfo : customerInfoList) {
                        for (CustomerInfo info : customerInfos) {
                            if (customerInfo.getCustomerCode().equals(info.getCustomerCode())) {
                                info.setSalesAmountJun24(customerInfo.getFY23_24SalesAmount());
                                break;
                            }
                        }
                    }
                    break;
                case 12:
                    for (CustomerInfo customerInfo : customerInfoList) {
                        for (CustomerInfo info : customerInfos) {
                            if (customerInfo.getCustomerCode().equals(info.getCustomerCode())) {
                                info.setSalesAmountJul24(customerInfo.getFY23_24SalesAmount());
                                break;
                            }
                        }
                    }
                    break;
                case 13:
                    for (CustomerInfo customerInfo : customerInfoList) {
                        for (CustomerInfo info : customerInfos) {
                            if (customerInfo.getCustomerCode().equals(info.getCustomerCode())) {
                                info.setSalesAmountAug24(customerInfo.getFY23_24SalesAmount());
                                break;
                            }
                        }
                    }
                    break;
                case 14:
                    for (CustomerInfo customerInfo : customerInfoList) {
                        for (CustomerInfo info : customerInfos) {
                            if (customerInfo.getCustomerCode().equals(info.getCustomerCode())) {
                                info.setSalesAmountSep24(customerInfo.getFY23_24SalesAmount());
                                break;
                            }
                        }
                    }
                    break;
                case 15:
                    for (CustomerInfo customerInfo : customerInfoList) {
                        for (CustomerInfo info : customerInfos) {
                            if (customerInfo.getCustomerCode().equals(info.getCustomerCode())) {
                                info.setSalesAmountOct24(customerInfo.getFY23_24SalesAmount());
                                break;
                            }
                        }
                    }
                    break;
                case 16:
                    for (CustomerInfo customerInfo : customerInfoList) {
                        for (CustomerInfo info : customerInfos) {
                            if (customerInfo.getCustomerCode().equals(info.getCustomerCode())) {
                                info.setSalesAmountNov24(customerInfo.getFY23_24SalesAmount());
                                break;
                            }
                        }
                    }
                    break;
                case 17:
                    for (CustomerInfo customerInfo : customerInfoList) {
                        for (CustomerInfo info : customerInfos) {
                            if (customerInfo.getCustomerCode().equals(info.getCustomerCode())) {
                                info.setSalesAmountDec24(customerInfo.getFY23_24SalesAmount());
                                break;
                            }
                        }
                    }
                    break;
                case 18:
                    for (CustomerInfo customerInfo : customerInfoList) {
                        for (CustomerInfo info : customerInfos) {
                            if (customerInfo.getCustomerCode().equals(info.getCustomerCode())) {
                                info.setSalesAmountJan25(customerInfo.getFY23_24SalesAmount());
                                break;
                            }
                        }
                    }
                    break;
                case 19:
                    for (CustomerInfo customerInfo : customerInfoList) {
                        for (CustomerInfo info : customerInfos) {
                            if (customerInfo.getCustomerCode().equals(info.getCustomerCode())) {
                                info.setSalesAmountFeb25(customerInfo.getFY23_24SalesAmount());
                                break;
                            }
                        }
                    }
                    break;
                case 20:
                    for (CustomerInfo customerInfo : customerInfoList) {
                        for (CustomerInfo info : customerInfos) {
                            if (customerInfo.getCustomerCode().equals(info.getCustomerCode())) {
                                info.setSalesAmountMar25(customerInfo.getFY23_24SalesAmount());
                                break;
                            }
                        }
                    }
                    break;
                case 21:
                    for (CustomerInfo customerInfo : customerInfoList) {
                        for (CustomerInfo info : customerInfos) {
                            if (customerInfo.getCustomerCode().equals(info.getCustomerCode())) {
                                info.setSalesAmountApr25(customerInfo.getFY23_24SalesAmount());
                                break;
                            }
                        }
                    }
                    break;
                case 22:
                    for (CustomerInfo customerInfo : customerInfoList) {
                        for (CustomerInfo info : customerInfos) {
                            if (customerInfo.getCustomerCode().equals(info.getCustomerCode())) {
                                info.setSalesAmountMay25(customerInfo.getFY23_24SalesAmount());
                                break;
                            }
                        }
                    }
                    break;
                case 23:
                    for (CustomerInfo customerInfo : customerInfoList) {
                        for (CustomerInfo info : customerInfos) {
                            if (customerInfo.getCustomerCode().equals(info.getCustomerCode())) {
                                info.setSalesAmountJun25(customerInfo.getFY23_24SalesAmount());
                                break;
                            }
                        }
                    }
                    break;

                case 24:
                    for (CustomerInfo customerInfo : customerInfoList) {
                        for (CustomerInfo info : customerInfos) {
                            if (customerInfo.getCustomerCode().equals(info.getCustomerCode())) {
                                info.setSalesAmountJul25(customerInfo.getFY23_24SalesAmount());
                                break;
                            }
                        }
                    }
                    break;
                case 25:
                    for (CustomerInfo customerInfo : customerInfoList) {
                        for (CustomerInfo info : customerInfos) {
                            if (customerInfo.getCustomerCode().equals(info.getCustomerCode())) {
                                info.setSalesAmountAug25(customerInfo.getFY23_24SalesAmount());
                                break;
                            }
                        }
                    }
                    break;
                case 26:
                    for (CustomerInfo customerInfo : customerInfoList) {
                        for (CustomerInfo info : customerInfos) {
                            if (customerInfo.getCustomerCode().equals(info.getCustomerCode())) {
                                info.setSalesAmountSep25(customerInfo.getFY23_24SalesAmount());
                                break;
                            }
                        }
                    }
                    break;
                case 27:
                    for (CustomerInfo customerInfo : customerInfoList) {
                        for (CustomerInfo info : customerInfos) {
                            if (customerInfo.getCustomerCode().equals(info.getCustomerCode())) {
                                info.setSalesAmountOct25(customerInfo.getFY23_24SalesAmount());
                                break;
                            }
                        }
                    }
                    break;
                case 28:
                    for (CustomerInfo customerInfo : customerInfoList) {
                        for (CustomerInfo info : customerInfos) {
                            if (customerInfo.getCustomerCode().equals(info.getCustomerCode())) {
                                info.setSalesAmountNov25(customerInfo.getFY23_24SalesAmount());
                                break;
                            }
                        }
                    }
                    break;
                case 29:
                    for (CustomerInfo customerInfo : customerInfoList) {
                        for (CustomerInfo info : customerInfos) {
                            if (customerInfo.getCustomerCode().equals(info.getCustomerCode())) {
                                info.setSalesAmountDec25(customerInfo.getFY23_24SalesAmount());
                                break;
                            }
                        }
                    }
                    break;
                case 30:
                    for (CustomerInfo customerInfo : customerInfoList) {
                        for (CustomerInfo info : customerInfos) {
                            if (customerInfo.getCustomerCode().equals(info.getCustomerCode())) {
                                info.setSalesAmountJan26(customerInfo.getFY23_24SalesAmount());
                                break;
                            }
                        }
                    }
                    break;
                case 31:
                    for (CustomerInfo customerInfo : customerInfoList) {
                        for (CustomerInfo info : customerInfos) {
                            if (customerInfo.getCustomerCode().equals(info.getCustomerCode())) {
                                info.setSalesAmountFeb26(customerInfo.getFY23_24SalesAmount());
                                break;
                            }
                        }
                    }
                    break;
                case 32:
                    for (CustomerInfo customerInfo : customerInfoList) {
                        for (CustomerInfo info : customerInfos) {
                            if (customerInfo.getCustomerCode().equals(info.getCustomerCode())) {
                                info.setSalesAmountMar26(customerInfo.getFY23_24SalesAmount());
                                break;
                            }
                        }
                    }
                    break;
                case 33:
                    for (CustomerInfo customerInfo : customerInfoList) {
                        for (CustomerInfo info : customerInfos) {
                            if (customerInfo.getCustomerCode().equals(info.getCustomerCode())) {
                                info.setSalesAmountApr26(customerInfo.getFY23_24SalesAmount());
                                break;
                            }
                        }
                    }
                    break;
                case 34:
                    for (CustomerInfo customerInfo : customerInfoList) {
                        for (CustomerInfo info : customerInfos) {
                            if (customerInfo.getCustomerCode().equals(info.getCustomerCode())) {
                                info.setSalesAmountMay26(customerInfo.getFY23_24SalesAmount());
                                break;
                            }
                        }
                    }
                    break;
                case 35:
                    for (CustomerInfo customerInfo : customerInfoList) {
                        for (CustomerInfo info : customerInfos) {
                            if (customerInfo.getCustomerCode().equals(info.getCustomerCode())) {
                                info.setSalesAmountJun26(customerInfo.getFY23_24SalesAmount());
                                break;
                            }
                        }
                    }
                    break;
            }
        }
        for (CustomerInfo customerInfo : customerInfos) {
            String followUpPerson = customerInfo.getFollowUpPerson();
            if (followUpPerson != null) {
                followUpPerson = followUpPerson.replaceAll("[^\\u4e00-\\u9fa5]", "").trim();
            } else {
                followUpPerson = ""; // 或者其他默认值
            }
            if (customerInfo.getCustomerLevelFY23_24()==null){
                customerInfo.setFY23_24SalesAmount(null);
            }
            customerInfo.setFollowUpPerson(followUpPerson);
            customerInfoMapper.insert(customerInfo);
        }
    }
}
